/**
 * Created by Zy on 2017/12/20.
 */
var URL = 'http://cqwind.chinacloudapp.cn:8381';
/*风场风机状态*/
Vue.component('chartline-component',{
    props:[''],
    'template':`
		<div>
			<div :id="chartId" class="chartBox" ></div>
		</div>
	`,
    data:function(){
        return {
            chartId:'chart_'+this.uuid(8, 64),
            myChart:null,
        }
    },
    watch:{

    },
    mounted:function(){
        var _this=this;
        this.$nextTick(function(){
            _this.init();
            setInterval(function(){
                _this.getFanstatus();
            },1000)
        });
    },
    methods:{
        uuid:function(len, radix) {
            //uuid函数，为chart设置id熟悉
            var chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split('');
            var uuid = [],
                i;
            radix = radix || chars.length;
            if (len) {
                // Compact form
                for (i = 0; i < len; i++) uuid[i] = chars[0 | Math.random() * radix];
            } else {
                // rfc4122, version 4 form
                var r;
                // rfc4122 requires these characters
                uuid[8] = uuid[13] = uuid[18] = uuid[23] = '-';
                uuid[14] = '4';
                // Fill in random data. At i==19 set the high bits of clock sequence as
                // per rfc4122, sec. 4.1.5
                for (i = 0; i < 36; i++) {
                    if (!uuid[i]) {
                        r = 0 | Math.random() * 16;
                        uuid[i] = chars[(i == 19) ? (r & 0x3) | 0x8 : r];
                    }
                }
            }
            return uuid.join('');
        },
        init:function(){
            var option={
                tooltip: {
                    trigger: 'axis',
                    axisPointer: {
                        type: 'line',
                        lineStyle: {
                            color: '#2f2f30',
                            width: 1,
                            type: 'solid'
                        }
                    }
                },
                legend: {
                    orient: 'vertical',
                    right: '20%',
                    itemWidth: 10,
                    itemHeight: 10,
                    data: [{
                        name: '风机风速',
                        icon: 'rect',
                        textStyle: {
                            color: '#ccc'
                        }
                    }, {
                        name: '累计发电量',
                        icon: 'rect',
                        textStyle: {
                            color: '#ccc'
                        }
                    }]
                },
                grid: {
                    left: '5%',
                    right: '4%',
                    bottom: '8%',
                    containLabel: true
                },
                axisLine: {
                    show: true,
                    lineStyle: {
                        color: '#2d2d2d',
                        width: 1,
                        type: 'solid'
                    }
                },
                axisLabel: {
                    show: false,
                    textStyle: {
                        color: '#fff'
                    }
                },
                xAxis: {
                    name: '时间',
                    type: 'category',
                    boundaryGap: false,
                    data:[],// data.T_Time,
                    /*['2017/3/15', '2017/3/16', '2017/3/17', '2017/3/18','2017/3/19', '2017/3/20', '2017/3/21'],*/
                    axisLine: {
                        lineStyle: {
                            color: '#2d2d2d'
                        }
                    },
                    nameTextStyle: {
                        color: '#fff'
                    }
                },
                yAxis: [
                    {
                    name:'风速(m/s)',
                    splitLine: {
                        show: true,
                        lineStyle: {
                            color: '#fff',
                            type: 'solid',
                            width: 1,
                            opacity: 0.1
                        }
                    },
                    //max:1,
                    axisLine: {
                        lineStyle: {
                            color: '#2e2d2e'
                        }
                    },
                    nameTextStyle: {
                        color: '#fff'
                    },
                    axisLabel: {
                        textStyle: {
                            color: '#e6e6e6'
                        }
                    }
                },
                    {
                        name:'累计发电量(kWh)',
                        nameTextStyle:{
                            color:'#fff'
                        },
                        splitLine:{
                            show:false,
                        },
                        position: 'right',
                        offset: 0,
                        axisLine: {
                            lineStyle: {
                                color: '#2e2d2e'
                            }
                        },
                        axisLabel: {
                            textStyle: {
                                color: '#fff'
                            }
                        }
                    }],
                /*
                 toolbox: {
                 show: true,
                 left:'45%',
                 top:'0%',
                 itemGap:15,
                 feature: {
                 dataZoom: {
                 yAxisIndex: 'none'
                 },
                 restore: {}
                 }
                 },*/
                series: [{
                    name: '风机风速',
                    type: 'line',
                    data: [],//data.historyinfo,
                    //[0.38, 0.39, 0.40, 0.41, 0.40,0.39,0.38],
                    itemStyle: {
                        normal: {
                            color: '#54abe8'
                        }
                    },
                    lineStyle:{
                        normal:{
                            color:'#54abe8',
                            type:'solid'
                        }
                    },
                    smooth: true
                },
                    {
                        name: '累计发电量',
                        type: 'line',
                        yAxisIndex: 1,
                        data:[]    //data.otherhistoryinfo
                        /*[0.39, 0.40, 0.41, 0.40, 0.39,0.38,0.37]*/,
                        itemStyle: {
                            normal: {
                                color: '#119c64'
                            }
                        },
                        smooth: true,
                        markLine: {
                            silent: true,
                            symbol: '',
                            large: true,
                            lineStyle: {
                                normal: {
                                    color: 'yellow',
                                    width: 1,
                                    type: 'dashed'
                                }
                            },
                            data: [
                                /*[{
                                 xAxis: '2017/3/20',
                                 yAxis: 0
                                 }, {
                                 xAxis: '2017/3/20',
                                 yAxis: 1
                                 }]*/
                            ]
                        }
                    }
                ]
            };
            this.myChart= echarts.init(document.getElementById(this.chartId));
            this.showLoad();
            this.myChart.setOption(option);
        },
        showLoad:function(){
            var loadingConfig={
                text: '',
                color: '#b0e9fe',
                textColor: '#000',
                maskColor: 'rgba(255, 255, 255, 0)',
                zlevel: 0
            };
            this.myChart.showLoading(loadingConfig);
        },
        hideLoad:function(){
            this.myChart.hideLoading();
        },
        loadData:function(data){
            this.myChart.setOption({
                xAxis: {
                    name: '时间',
                    type: 'category',
                    boundaryGap: false,
                    data:data.dateTime
                    /*['2017/3/15', '2017/3/16', '2017/3/17', '2017/3/18','2017/3/19', '2017/3/20', '2017/3/21'],*/
                },
                series: [
                    {
                        name: '风机风速',
                        type: 'line',
                        data: data.windSpeed5m
                        // data:[0.38, 0.39, 0.40, 0.41, 0.40,0.39,0.38],
                    },
                    {
                        name: '累计发电量',
                        type: 'line',
                        yAxisIndex: 1,
                        data:data.totalGenerating
                        // data:[0.39, 0.40, 0.41, 0.40, 0.39,0.38,0.37]

                    }
                ]
            })
        },
        getFanstatus:function(){
            var _this=this;
            var success=function(res){
                _this.loadData(res.data)
                _this.hideLoad();
            };
            var error = function(e){
                console.log('加载风速和累计发电量出错')
            }
            //this.$http(URL+'code=3').then(success,error)
            axios.get(URL+'?code=3').then(success).catch(error)

        }
    }

});

/*vue实例*/
var cps = new Vue({
    el:'#cps',
    data:{
        datalist:[],
        alarmStatus:null,//报警状态
        powerFactor:null,//功率因数
        lower:null,//发电损失
        powergrid:null,//电网调度匹配率
        fanUsage:null,//风机使用率
        powerGeneration:null,//累计发电量
        powerCost:null,//发电成本
        datatime:null,//当前时间
        RevolutionPerMin: 3,//风轮发电转速
        //单风机信息
        controlMode:null,//机组控制方式
        runMode:null,//机组运行模式
        runStatus:null,//机组运行状态
    },
    mounted:function(){
        var _this=this;
        this.$nextTick(function(){
            setInterval(function(){
                _this.getMessage();
                _this.changStutas();//风机报警状态
                _this.controlModeF();//机组控制方式
                _this.runModeF();//机组运行模式
                _this.runStatusF();//机组运行状态

                _this.lower = ((_this.rnd(0.015,0.025)*(_this.RevolutionPerMin))*100/((_this.RevolutionPerMin)+0.1)).toFixed(2);//发电损失率
                _this.powergrid =((_this.rnd(0.93,0.98)*(_this.RevolutionPerMin))*100/((_this.RevolutionPerMin)+2)).toFixed(1);
                //_this.powergrid = Math.round(_this.rnd(93,98));//风机电网调度匹配
                _this.fanUsage = Math.round(100);//风机使用率
                //_this.powerGeneration = Math.round(_this.rnd(88,100));//风机累计发电量
                _this.powerCost = _this.rnd(0.45,0.6);//发电成本
                _this.datatime = _this.getTime();
            },1000)

        })
    },
    methods: {
        //获取信息中心的数据
        getMessage: function () {
            var _this=this;
            axios.get(URL+'?code=1').then(function (res) {
                _this.datalist = res.data;
                console.log(typeof _this.RevolutionPerMin);
                _this.powerFactor = (_this.datalist.powerFactor*100).toFixed(0);//功率因素
                _this.RevolutionPerMin=Number(_this.datalist.revolutionPerMin);//风轮发电转速
                _this.bladeAngle = (_this.datalist.bladeAngle);//叶片角度
                _this.outputPower = (_this.datalist.outputPower);//输出功率
                _this.windDirection = (_this.datalist.windDirection);//实测风向
                _this.windSpeed5m = (_this.datalist.windSpeed5m);//风速
                _this.powerGeneration = Number(_this.datalist.totalGenerating).toFixed(2)//累计发电量
            })
        },
        //风机报警状态
        changStutas:function(){
            var _this=this;
            switch (_this.datalist.alarmStatus){
                case '1':
                    _this.alarmStatus='正常';
                    break;
                case '2':
                    _this.alarmStatus='警告';
                    break;
                case '3':
                    _this.alarmStatus='故障';
                    break;
            }
        },
        //机组控制方式
        controlModeF:function(){
            var _this= this;
            switch(_this.datalist.controlMode){
                case '1':
                    _this.controlMode='本地方式';
                    break;
                case '2':
                    _this.controlMode = '远程方式';
                    break;
            }
        },
        //机组运行模式
        runModeF:function(){
            var _this = this;
            switch(_this.datalist.runMode){
                case '1':
                    _this.runMode = '手动模式';
                break;
                case '2':
                    _this.runMode = '自动模式';
                    break;
                case '3':
                    _this.runMode = '维护模式';
                    break;
                case '4':
                    _this.runMode='停机模式';
                    break;
            }
        },
        //机组运行状态
        runStatusF:function(){
            var _this = this;
            switch(_this.datalist.runStatus){
                case '1':
                    _this.runStatus='启 动';
                    break;
                case '2':
                    _this.runStatus='运 行';
                    break;
                case '3':
                    _this.runStatus='暂 停';
                    break;
                case '4':
                    _this.runStatus='停 机';
                    break;
                case '5':
                    _this.runStatus='急 停';
                    break;
                case '6':
                    _this.runStatus='空 转';
                    break;

            }
        },
        /*两数之间的随机数*/
        rnd: function (min, max) {
            return (Math.random()*(max - min) + min).toFixed(2);
        },
        /*获取当前之间*/
        getTime:function(){
            var oData = new Date();
            return (oData.getFullYear()+'年'+(oData.getMonth()+1)+'月'+oData.getDate()+'日')
        }
    }
});